package com.chenkun.service;

import com.chenkun.common.define.CommRes;
import com.chenkun.common.enums.JobType;
import com.chenkun.dao.JobDao;
import com.chenkun.pojo.JobInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * Created by chenkun on 2017/5/14.
 */
@Service
public class JobService {

    @Autowired
    private JobDao jobDao;

    /**
     * 获取所有有效职务
     * @return
     */
    public CommRes getAllJobs(){
        List<JobInfo> list = this.jobDao.getAllUsefullJobs();
        if (list.size()==0 || list==null){
            return CommRes.error("-4","无数据或查询出错");
        }
        return CommRes.success(list);
    }

    /**
     * 添加职务
     * @param jobInfo
     * @return
     */
    public CommRes addJob(JobInfo jobInfo){
        List<JobInfo> list = this.jobDao.getAllUsefullJobs();
        JobInfo top_job = this.jobDao.getJobByCode(jobInfo.getTop_code());
        if (top_job == null || top_job.getType_code()== JobType.DUTIES.getCode()){
            return CommRes.error("-2","上级编码有误");
        }
        for (JobInfo metaJob : list){
            if ((jobInfo.getUse_date().before(metaJob.getOverdue_date()) && jobInfo.getName().equals(metaJob.getName())) || jobInfo.getCode()==metaJob.getCode()){
                return CommRes.error("-3","名称重复或时间交叉或编号重复");
            }
        }
        int res = this.jobDao.insert(jobInfo);
        if (res <= 0){
            return CommRes.error("-4","添加失败");
        }
        return CommRes.success(jobInfo);
    }

    /**
     * 根据id获取职务
     * @param id
     * @return
     */
    public CommRes getById(int id){
        JobInfo jobInfo = this.jobDao.getById(id);
        if (jobInfo == null){
            return CommRes.error("-4","未查询到相关编码职务");
        }
        return CommRes.success(jobInfo);
    }

    /**
     * 删除职务
     * @param code
     * @return
     */
    public CommRes deleteJob(int code){
        List<JobInfo> list = this.jobDao.getAllUsefullJobs();
        for (JobInfo metaJob : list){
            if (metaJob.getTop_code()==code){
                return CommRes.error("-5","该分类有下级，删除失败");
            }
        }
        int res = this.jobDao.delete(code);
        if (res<=0){
            return CommRes.error("-4","删除失败");
        }
        return CommRes.success();
    }

    /**
     * 修改职务信息
     * @param jobInfo
     * @return
     */
    public CommRes updateJob(JobInfo jobInfo){
        JobInfo top_job = this.jobDao.getJobByCode(jobInfo.getTop_code());
        if (top_job == null || top_job.getType_code()== JobType.DUTIES.getCode()){
            return CommRes.error("-2","上级编码有误");
        }
        JobInfo job = this.jobDao.getById(jobInfo.getId());
        if (!job.getName().equals(jobInfo.getName())){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,-1);
            job.setOverdue_date(calendar.getTime());
            this.jobDao.update(job);
            jobInfo.setUse_date(new java.util.Date());
        }
        int res = this.jobDao.insert(jobInfo);
        if (res<=0 ){
            return CommRes.error("-4","修改失败");
        }
        return CommRes.success(jobInfo);
    }

}
